<template>
  <div class="create-order">
    <Header title="生成订单" />
    <van-contact-card type="edit" :tel="tel" :name="name" @click="onEdit" />
    <div class="content">
      <div v-for="(item, index) in store.state.orderList" :key="index">
        <van-card
          :num="item.num"
          :price="item.price"
          :title="item.title"
          :thumb="item.pic"
        />
      </div>
    </div>
    <div class="pay-wrap">
      <div class="price">
        <span>商品金额</span>
        <span>￥{{ initprice }}</span>
      </div>
      <div class="pay-btn">
        <van-button type="primary" block color="#ffc400" @click="createOrder"
          >生成订单</van-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import Header from "../../components/Header.vue";
import { onMounted, reactive, toRefs } from "vue";
import { useStore } from "vuex";
import { useRouter, useRoute} from "vue-router";
import { showDialog } from 'vant';
import 'vant/es/dialog/style';
export default {
  components: { Header },
  setup() {
    const store = useStore();
    const router = useRouter()
    const route = useRoute()
    const data = reactive({
      tel: "13694905768",
      name: "黎家伟",
      initprice: [],
    });
    const initpay = () => {
      let price = 0;
      if (store.state.orderList.length) {
        store.state.orderList.forEach((item) => {
          price += item.num * item.price;
        });
        data.initprice = price;
      }
    };
    const initUser = ()=>{
        store.state.userAddress.forEach(item=>{
            if(item.isDefault){
                data.tel = item.tel
                data.name = item.name
            }
        })
    }
    const onEdit = () => {
      router.push('/address')
    };
    const createOrder = () => {
      showDialog({
        title: "提示",
        message: "生成订单成功",
      }).then(() => {
        // 要跟购物车联系起来：提交订单的时候，购物车要减除所提交的订单
        let newList = store.state.cartSum.filter((item)=>{
          return !route.query.list.includes(item.id+"")
        })
        
        store.commit('deleteList',newList);
        store.commit('orderListEnd');

        // console.log(route.query.list);
        // console.log(newList);
        router.push('/order');
      });
    };
    onMounted(() => {
      initpay();
      initUser()
      // console.log(store.state.orderList);
    });
    return {
      ...toRefs(data),
      store,
      onEdit,
      createOrder,
    };
  },
};
</script>

<style lang="less" scoped>
.create-order {
  display: flex;
  flex-flow: column;
  height: 100%;
  .content {
    flex: 1;
    overflow-y: auto;
  }
  .pay-wrap {
    padding: 10px 0;
    width: 100%;
    background: #fff;
    border-top: 1px solid #e9e9e9;
    > div {
      display: flex;
      justify-content: space-between;
      padding: 0 5%;
      margin: 10px 0;
      font-size: 14px;
      span:nth-child(2) {
        color: red;
        font-size: 18px;
      }
    }
    .pay-btn {
      width: 90%;
      margin: 0 auto;
    }
  }
}
</style>